'''
json数据和python字典的相互转换
'''
import json

# 创建一个列表，列表中都是字典
data = [{"name": "张三", "age": 16, "sex": "男"}, {"name": "李四", "age": 17, "sex": "男"},
        {"name": "小红", "age": 16, "sex": "女"}]
# 列表转换未json
json_str = json.dumps(data, ensure_ascii=False)
print(json_str)

# 字典转换为json
data1 = {"name": "张三", "age": 16, "sex": "男"}
json_str1 = json.dumps(data1, ensure_ascii=False)
print(json_str1)

# json转换为python数据类型
js = '[{"name": "张三", "age": 16, "sex": "男"}, {"name": "李四", "age": 17, "sex": "男"},{"name": "小红", "age": 16, "sex": "女"}]'
py = json.loads(js)
print(py)

#json转换为字典
js1 = '{"name": "张三", "age": 16, "sex": "男"}'
py1 = json.loads(js1)
print(py1)